#include "AcronymDictionary.h"
#include <xmllite.h>

AcronymDictionary::AcronymDictionary(void)
{
}

AcronymDictionary::~AcronymDictionary(void)
{
	for( unsigned int i = 0; i < Items.size(); ++i)
	{
		delete Items[i];
	}
}

int AcronymDictionary::Add(const CString& key)
{
	Acronym* pAcronym = new Acronym();
	pAcronym->Key = key;
	
	Items.push_back(pAcronym);

	return Items.size() - 1;
}


bool AcronymDictionary::Load(const CString& configFile)
{
	HRESULT hr;
	CComPtr<IStream> pFileStream;
	CComPtr<IXmlReader> pReader;
	XmlNodeType nodeType;
	const WCHAR* pName;
	const WCHAR* pValue;
 
	Items.clear();
   
	//Open XML document
	if (FAILED(hr = SHCreateStreamOnFile(configFile,  STGM_READ, &pFileStream)))
	{
		wprintf(L"Error opening XML document, error %08.8lx", hr);
		return false;
	}

	if (FAILED(hr = CreateXmlReader(__uuidof(IXmlReader), (void**)&pReader, NULL)))
	{
		wprintf(L"Error creating XmlReader, error %08.8lx", hr);
		return false;
	}

	if (FAILED(hr = pReader->SetInput(pFileStream)))
	{
		wprintf(L"Error setting input for XmlReader, error %08.8lx", hr);
		return false;
	}

	Acronym* pAcronym = 0;

	while (S_OK == (hr = pReader->Read(&nodeType))) 
	{
		switch (nodeType)
		{
			case XmlNodeType_Element:
				if (FAILED(hr = pReader->GetQualifiedName(&pName, NULL)))                      
				{
					wprintf(L"Error reading element name, error %08.8lx", hr);
					return false;
				}
				
				if (wcscmp(pName, L"acronym") == 0)
				{
					if (FAILED(hr = pReader->MoveToAttributeByName(L"text", NULL)))
					{
						continue;
					}
					if (FAILED(hr = pReader->GetValue(&pValue, NULL)))                      
					{
						wprintf(L"Error reading attribute value, error %08.8lx", hr);
						continue;
					}

					if(pAcronym) delete pAcronym;

					pAcronym = new Acronym();
					
					pAcronym->Key = CString(pValue);
					
				}	
					
				if (wcscmp(pName, L"value") == 0)
				{
					if (FAILED(hr = pReader->MoveToAttributeByName(L"text", NULL)))                      
					{
						wprintf(L"Error reading attribute \"value\", error %08.8lx", hr);
						continue;
					}
						
					if (FAILED(hr = pReader->GetValue(&pValue, NULL)))                      
					{
						wprintf(L"Error reading attribute value, error %08.8lx", hr);
						continue;
					}
					
					if(pAcronym)
						pAcronym->Values.push_back(CString(pValue));
				}
			break;
				
			case XmlNodeType_EndElement:
				if (FAILED(hr = pReader->GetQualifiedName(&pName, NULL)))                      
				{
					wprintf(L"Error reading element name, error %08.8lx", hr);
					return false;
				}

				if (wcscmp(pName, L"acronym") != 0) continue;

				if(pAcronym)
				{
					Items.push_back(pAcronym);
					pAcronym = 0;
				}


			break;
		}
	}
	return true;
}
int AcronymDictionary::IndexOf(const CString& str)
{
	for(unsigned int i = 0; i < Items.size(); ++i)
	{
		if(Items[i]->Key == str) return i;
	}
	return -1;
}